package com.ztjy.purchase.controller;

import com.github.pagehelper.PageInfo;
import com.ztjy.annonation.MyLog;
import com.ztjy.bean.CBrand;
import com.ztjy.bean.CProduct;
import com.ztjy.bean.CType;
import com.ztjy.purchase.biz.CBrandBiz;
import com.ztjy.purchase.biz.CProductBiz;
import com.ztjy.purchase.biz.CTypeBiz;
//import javafx.scene.control.Alert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/purchase/productType")
@SessionAttributes({"cblist","date1"})
public class CTypeControler {
    @Autowired
    private CTypeBiz tbiz;
    @Autowired
    private CBrandBiz bbiz;
    @Autowired
    private CProductBiz cpbiz;

    //每次执行前会优先 将mid放入request范围
    @ModelAttribute
    public void initModuleData(HttpSession session) {
        session.removeAttribute("mid");
        session.setAttribute("mid", 18);
    }

    @MyLog(value = "查询所有商品类型操作")
    @RequestMapping("/ctypeAll")
    public String cTypeAll(Model model, CType cType,@RequestParam(value = "pageno",defaultValue = "1") Integer pageNo){
        Integer pageSize=6;
        PageInfo<CType> allCType = tbiz.findAllCType(cType, pageNo, pageSize);
        model.addAttribute("ctpage",allCType);
        model.addAttribute("ctype",cType);
        List<CBrand> allCBrand = bbiz.findAllCBrand();
        model.addAttribute("cblist",allCBrand);
        Date date = new Date();
        model.addAttribute("date1",date);
        return "forward:/purchase/productType/productTypeList.html";
    }

    @MyLog(value = "品牌类型添加操作")
    @RequestMapping("/ctypeAdd")
    public String addCType(Model model,CType cType){
        boolean flag = tbiz.addCType(cType);
        if (flag){
            return "redirect:/purchase/productType/ctypeAll";
        }else {
            return "forward:/purchase/productType/productTypeAdd.html";
        }
    }

    @MyLog(value = "跳入品牌类型修改页面")
    @RequestMapping("/goCTypeUpdate/{tid}")
    public String goCTypeUpdate(Model model,@PathVariable("tid") Integer tid){
        CType cTypeByTid = tbiz.findCTypeByTid(tid);
        model.addAttribute("uptype",cTypeByTid);
        return "forward:/purchase/productType/productTypeUpdate.html";
    }

    @MyLog(value = "品牌类型修改操作")
    @RequestMapping("/ctypeUpdate")
    public String doUpdateCType(Model model,CType cType){
        boolean flag = tbiz.updateCType(cType);
        if (flag){
            return "redirect:/purchase/productType/ctypeAll";
        }else {
            return "forward:/purchase/productType/productTypeUpdate.html";
        }
    }

    /**
     *
     * @param tid
     * @param tStatus
     * @return
     */
    @MyLog(value = "商品类型状态注销/恢复操作")
    @RequestMapping("/updateCTypeState")
    public String updateCTypeState(Integer tid, Integer tStatus){
        CType cType = tbiz.findCTypeByTid(tid);
        if(cType.getTypeStatus()==1){
            cType.setTypeStatus(2);
            List<CProduct> cProducts = cpbiz.findbyTypeId(tid);
            for(CProduct p:cProducts){
                p.setProductStatus(2);
                cpbiz.updateProduct(p);
            }
        }else if(cType.getTypeStatus()==2&&cType.getCBrand().getBrandStatus()==1){
            cType.setTypeStatus(1);
        }
        tbiz.updateCType(cType);
        return "redirect:/purchase/productType/ctypeAll";
    }
}
